package com.yunxi.service.dubboService.impl.sto;

import com.github.pagehelper.PageInfo;
import com.yunxi.service.localService.sto.DailyReportService;
import com.yunxi.sto.pojo.DailyReportVO;
import com.yunxi.sto.pojo.MonthlyReportVO;
import com.yunxi.sto.pojo.YearReportVO;
import com.yunxi.sto.service.DailyReportDubboService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 基础报告 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-09-20
 */
public class DailyReportDubboServiceImpl implements DailyReportDubboService {

    @Autowired
    @Qualifier(value = "dailyReportServiceImpl")
    DailyReportService dailyReportService;

    /**
     * 日报表
     * @param dailyReportVO
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<DailyReportVO> findPageByVO(DailyReportVO dailyReportVO, Integer pageNum, Integer pageSize)
        throws Exception {
        if (StringUtils.isEmpty(dailyReportVO.getQueryDate())) {
            throw new Exception("操作失败,请选择查询日期!");
        }
        PageInfo<DailyReportVO> pageInfo = dailyReportService.pageByVo(dailyReportVO, pageNum, pageSize);
        List<DailyReportVO> list = pageInfo.getList();
        Double defaultValue = 0.00;
        if (list != null && list.size() > 0) {
            for (DailyReportVO vo : list) {
                if (vo.getAllClueCount() == 0) {
                    vo.setHighCluePercent(defaultValue);//高意向线索率
                    vo.setVisitAgainPercent(defaultValue);//再次邀约率
                    vo.setTestDrivePercent(defaultValue);//今日试驾率
                    vo.setBorderPercent(defaultValue);//今日大定率
                    vo.setHandlePercent(defaultValue);//今日交车率
                } else {
                    //高意向线索率
                    Integer newClueaCount = vo.getNewClueaCount();//A级线索
                    Integer newCluehCount = vo.getNewCluehCount();//H级线索
                    Double hcp = (double) (newClueaCount + newCluehCount) / vo.getAllClueCount();
                    vo.setHighCluePercent(hcp);

                    //再次邀约率
                    Integer visitAgainCount = vo.getVisitAgainCount();//再次邀约数
                    Double vap = (visitAgainCount.doubleValue() / vo.getAllClueCount());
                    vo.setVisitAgainPercent(vap);

                    //今日试驾率
                    Integer newDriveCount = vo.getNewDriveCount();//今日试驾
                    Double tdp = (newDriveCount.doubleValue() / vo.getAllClueCount());
                    vo.setTestDrivePercent(tdp);

                    //今日大定率
                    Integer newBorderCount = vo.getNewBorderCount();//大定数
                    Double bop = (newBorderCount.doubleValue() / vo.getAllClueCount());
                    vo.setBorderPercent(bop);

                    //今日交车率
                    if (newBorderCount != 0) {
                        Integer newHandleCount = vo.getNewHandleCount();//今日交车
                        Double hp = (newHandleCount.doubleValue() / newBorderCount);
                        vo.setHandlePercent(hp);
                    } else {
                        vo.setHandlePercent(defaultValue);
                    }

                }
            }
        }
        return pageInfo;
    }

    @Override
    public PageInfo<MonthlyReportVO> monthPage3(MonthlyReportVO monthlyReportVO, Integer pageNum, Integer pageSize) throws Exception {
        if(StringUtils.isEmpty(monthlyReportVO.getQueryDate()))throw new Exception("操作失败,请选择查询日期!");
        String curMonth = getCurMonth(monthlyReportVO.getQueryDate());
        monthlyReportVO.setCurYearMonth(curMonth);
        return dailyReportService.monthPage3(monthlyReportVO,pageNum,pageSize);
    }

    @Override
    public PageInfo<DailyReportVO> dayPage(DailyReportVO dailyReportVO, Integer pageNum, Integer pageSize) throws  Exception  {
        if(StringUtils.isEmpty(dailyReportVO.getQueryDate()))throw new Exception("操作失败,请选择查询日期!");

        return dailyReportService.dayPage(dailyReportVO,pageNum,pageSize);
    }

    @Override
    public void dayCalcMain(DailyReportVO dailyReportVO) throws SQLException {
        dailyReportService.dayCalcMain(dailyReportVO);
    }

    @Override
    public PageInfo<YearReportVO> yearList3(YearReportVO yearReportVO, Integer pageNum, Integer pageSize) throws Exception {
        if(StringUtils.isEmpty(yearReportVO.getQueryDate()))throw new Exception("操作失败,请选择查询日期!");
        yearReportVO.setCurYear(yearReportVO.getQueryDate().substring(0,4));
        return dailyReportService.yearList3(yearReportVO, pageNum, pageSize);
    }

    @Override
    public void monthCalcMain(MonthlyReportVO monthlyReportVO) throws Exception {
        if(StringUtils.isEmpty(monthlyReportVO.getQueryDate()))throw new Exception("操作失败,请选择查询日期!");
        //1、查询当前日期所在月的配置信息，如果存在进行统计
        Long target = dailyReportService.monthTargetAmount(monthlyReportVO.getQueryDate());
        Long orderTarget = dailyReportService.monthTargetOrderAmount(monthlyReportVO.getQueryDate());
        if(target==null) target = 0l;
        if(orderTarget==null) orderTarget = 0l;

        String curMonth = getCurMonth(monthlyReportVO.getQueryDate());
        monthlyReportVO.setCurYearMonth(curMonth);
        monthlyReportVO.setTargetAmount(target);
        monthlyReportVO.setTargetOrderAmount(orderTarget);
        dailyReportService.monthCalcMain(monthlyReportVO);
    }


    @Override
    public void yearCalcMain(YearReportVO yearReportVO) throws Exception {
        if(StringUtils.isEmpty(yearReportVO.getQueryDate()))throw new Exception("操作失败,请选择查询日期!");
        yearReportVO.setCurYear(yearReportVO.getQueryDate().substring(0,4));
        dailyReportService.yearCalcMain(yearReportVO);
    }

    public String getCurMonth(String dateStr) throws ParseException {
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr );
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int day = ca.get(Calendar.DAY_OF_YEAR);//一年中的第几天
        int week = ca.get(Calendar.WEEK_OF_YEAR);//一年中的第几周
        int month = ca.get(Calendar.MONTH)+1;//第几个月
        int year = ca.get(Calendar.YEAR);//年份数值
        if(month>9){
            return year+"-"+month+"";
        }
        return year+"-"+"0"+month;
    }
}
